﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Task.Data;
using CentralOffice.Task.DataAccess;
using CentralOffice.Task.BusinessActions;

using TaskTable = CentralOffice.Task.Data.TaskDataSet.TasksDataTable;
using TaskRow = CentralOffice.Task.Data.TaskDataSet.TasksRow;


namespace CentralOffice.WebClient.Tasks
{
    public partial class ManageMyTasks : PageBase
    {
        private bool? authorized = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                SiteUtil.PopulateListBox(ddlPriority, SiteConstants.TaskPriority, true);
                ddlPriority.SelectedIndex = 0;

                SiteUtil.PopulateListBox(ddlStatus, SiteConstants.TaskStatus, true);
            }

            if (authorized == null)
            {
                authorized = this.Authorize(AuthorizationRules.Task_Update);
            }
            if (!authorized.Value)
            {
                //gv.Columns.RemoveAt(gv.Columns.Count - 1);
                //gv.Columns.RemoveAt(gv.Columns.Count - 1);
            }
        }

        protected void odsTask_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            //if (!e.ExecutingSelectCount)
            //{
            //    //e.Arguments
            //}
            if (e.InputParameters["whereClause"] == null || e.InputParameters["whereClause"].ToString() == "")
            {
                //e.InputParameters["whereClause"] = task.FilterExpression;

                //IOrderedDictionary parameters = e.InputParameters;
                TaskRow task = (new TaskDataSet()).Tasks.NewTasksRow();
                task.Accepter = CommonUtil.TrimAll(ApplicationContext.ActualUserName);
                //parameters["whereClause"] = task.FilterExpression;
                e.InputParameters["whereClause"] = task.FilterExpression;
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gv.PageIndex = 0;

            TaskRow task = (new TaskDataSet()).Tasks.NewTasksRow();

            task.Name = txtName.Text;
            if (ddlCategory.SelectIndex > 0 && ddlCategory.SelectId != null)
            {
                task.CategoryId = ddlCategory.SelectId.Value;
            }

            if (ddlPriority.SelectedIndex > 0)
            {
                task.Significant = ddlPriority.SelectedItem.Text;
            }
            if (ddlStatus.SelectedIndex > 0)
            {
                task.Status = ddlStatus.SelectedItem.Text;
            }
            task.Sponsor = txtSponsor.Text;
            task.Accepter = CommonUtil.TrimAll(ApplicationContext.ActualUserName);

            if (txtIntendingDate.Text.Length > 0)
            {
                task.IntendingDate = DateTime.Parse(txtIntendingDate.Text);
            }
            if (txtApprovedDate.Text.Length > 0)
            {
                task.ApprovedDate = DateTime.Parse(txtApprovedDate.Text);
            }
            //task.CloseDate = txtCloseDate.Text;
            //task.Profile = txtProfile.Text;

            odsTask.SelectParameters.Clear();
            odsTask.SelectParameters.Add("whereClause", task.FilterExpression);
            //odsTask.SelectParameters["whereClause"].DefaultValue = task.FilterExpression;//companyInfo.FilterExpression
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TaskRow row = ((DataRowView)e.Row.DataItem).Row as TaskRow;
                TableCell statusCell = e.Row.Cells[7];
                if (row.Status == "执行中")
                {
                    statusCell.Style.Add(HtmlTextWriterStyle.Color, "#f00");
                }
                else if (row.Status == "已完成")
                {
                    statusCell.Style.Add(HtmlTextWriterStyle.Color, "#00f");
                }
                else if (row.Status == "确实完成")
                {
                    statusCell.Style.Add(HtmlTextWriterStyle.Color, "#0f0");
                }


            }
        }

    }
}
